package com.car.rental.auth.controller;

import com.car.rental.auth.model.request.LoginRequest;
import com.car.rental.auth.model.request.RegisterRequest;
import com.car.rental.auth.model.response.LoginResponse;
import com.car.rental.auth.service.AuthService;
import com.car.rental.common.core.response.ResponseResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ResponseResult<Void> register(@Valid @RequestBody RegisterRequest registerRequest) {
        log.info("收到注册请求: username={}", registerRequest.getUsername());
        
        // 参数校验日志
        log.info("注册参数: username={}, password长度={}, confirmPassword长度={}", 
            registerRequest.getUsername(),
            registerRequest.getPassword().length(),
            registerRequest.getConfirmPassword().length()
        );
        
        ResponseResult<Void> result = authService.register(registerRequest);
        log.info("注册结果: code={}, message={}", result.getCode(), result.getMessage());
        
        return result;
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseResult<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        log.info("收到登录请求: username={}", loginRequest.getUsername());
        
        ResponseResult<LoginResponse> result = authService.login(loginRequest);
        
        if (result.getCode() == 200) {
            log.info("登录成功: username={}", loginRequest.getUsername());
        } else {
            log.warn("登录失败: username={}, message={}", 
                loginRequest.getUsername(),
                result.getMessage()
            );
        }
        
        return result;
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ResponseResult<Void> logout(@RequestHeader("Authorization") String token) {
        return authService.logout(token);
    }

    /**
     * 验证token
     */
    @GetMapping("/validate")
    public ResponseResult<Boolean> validateToken(@RequestHeader(value = "Authorization", required = false) String token) {
        // 只在debug级别输出token信息
        if (log.isDebugEnabled()) {
            log.debug("收到token验证请求: token={}", token);
        }
        
        // 1. 检查token是否为空
        if (!StringUtils.hasText(token)) {
            return ResponseResult.error("token不能为空");
        }

        // 2. 处理Bearer前缀
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        ResponseResult<Boolean> result = authService.validateToken(token);
        
        // 只在debug级别输出验证结果
        if (log.isDebugEnabled()) {
            log.debug("token验证结果: valid={}", result.getData());
        }
        
        return result;
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public ResponseResult<LoginResponse> getUserInfo(@RequestHeader("Authorization") String token) {
        log.info("获取用户信息: token={}", token);
        
        ResponseResult<LoginResponse> result = authService.getUserInfo(token);
        
        if (result.getCode() == 200) {
            log.info("获取用户信息成功: username={}, userId={}", 
                result.getData().getUsername(),
                result.getData().getUserId()
            );
        } else {
            log.warn("获取用户信息失败: message={}", result.getMessage());
        }
        
        return result;
    }
} 